package leetcode.code;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author : zx
 * @version V1.0
 */
public class 两个数组的交集II {

    public static int[] intersect(int[] nums1, int[] nums2) {
        ArrayList<Integer> tmp = new ArrayList<>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int j = 0;
        for (int i : nums1){
            while (j < nums2.length && i > nums2[j]){
                j++;
            }
            if (j < nums2.length && i == nums2[j]){
                tmp.add(i);
                j++;
            }
        }
        Integer[] res = new Integer[tmp.size()];
        tmp.toArray(res);
        return Arrays.stream(res).mapToInt(Integer::valueOf).toArray();
    }

    public static void main(String[] args){
        int[] a = new int[]{4,9,5};
        int[] b = new int[]{9,4,9,8,4};
        System.out.println(Arrays.toString(intersect(a,b)));
        System.out.println(Arrays.toString(intersect(b,a)));
    }
}
